/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;
import entidades.*;
import Controladores.*;
import Controladores.exceptions.NonexistentEntityException;
import Controladores.exceptions.PreexistingEntityException;
import java.awt.Dimension;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
 *
 * @author Gatoper
 */
public class JPUVeterinarian extends javax.swing.JPanel {
    private Veterinarian Vet;

    /**
     * Creates new form JPUVeterinarian
     */
    public JPUVeterinarian() {
        initComponents();
        this.setSize(new Dimension(300, 400));
    }

    public JPUVeterinarian(Veterinarian vet) {
        this.Vet=vet;
        initComponents();
        String[] bday=vet.getBirthday().split(" ");
        String[] fechas=bday[0].split("-");
        CBDay.setSelectedIndex(Integer.valueOf(fechas[2])-1);
        CBmonth.setSelectedIndex(Integer.valueOf(fechas[1])-1);
        CByear.setSelectedIndex(Integer.valueOf(fechas[0])-1991);
        TFocusofPractice.setText(vet.getFocusofpractice());
        this.TEspecialities.setText(vet.getEspecialitie());
        TGraduatedAt.setText(vet.getGraduatedAt());
        TSSN.setText(vet.getSsn());
        TSalary.setText(vet.getSalary().toString());
        Tbirthplace.setText(vet.getBirthplace());
        Tlastname.setText(vet.getLasname());
        Tname.setText(vet.getName());
        EntityManagerFactory emf=Persistence.createEntityManagerFactory("HPVAS");
        VeterinarianJpaController VeterinJpaC=new VeterinarianJpaController(emf);
        Lpetnames.setText("Atiende a las mascotas: "+VeterinJpaC.findPetNames(vet.getName()));
        this.setSize(new Dimension(300, 400));
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TEspecialities = new javax.swing.JTextField();
        LEspecialities = new javax.swing.JLabel();
        TSalary = new javax.swing.JTextField();
        LSalary = new javax.swing.JLabel();
        TFocusofPractice = new javax.swing.JTextField();
        LFocusofPractice = new javax.swing.JLabel();
        LGraduatedAt = new javax.swing.JLabel();
        TGraduatedAt = new javax.swing.JTextField();
        Tbirthplace = new javax.swing.JTextField();
        Tname = new javax.swing.JTextField();
        CBDay = new javax.swing.JComboBox();
        CByear = new javax.swing.JComboBox();
        Llastname = new javax.swing.JLabel();
        TSSN = new javax.swing.JTextField();
        Lbirthday = new javax.swing.JLabel();
        CBmonth = new javax.swing.JComboBox();
        Lname = new javax.swing.JLabel();
        LSSN = new javax.swing.JLabel();
        Tlastname = new javax.swing.JTextField();
        Lbirthplace = new javax.swing.JLabel();
        Bupdate = new javax.swing.JButton();
        Bdelete = new javax.swing.JButton();
        Lpetnames = new javax.swing.JLabel();

        LEspecialities.setText("Especialities :");

        Snumero(TSalary);

        LSalary.setText("Salary :");

        TFocusofPractice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TFocusofPracticeActionPerformed(evt);
            }
        });

        LFocusofPractice.setText("Focus of Practice :");

        LGraduatedAt.setText("Graduated At :");

        Sletras(TGraduatedAt);

        Sletras(Tbirthplace);

        Sletras(Tname);
        Tname.setEnabled(false);
        Tname.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TnameActionPerformed(evt);
            }
        });
        Tname.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent evt) {
                TIdFocusGained(evt);
            }
        });

        CBDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4","5", "6", "7", "8","9", "10", "11", "12" }));

        CByear.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1991", "1992", "1993", "1994","1995", "1996", "1997", "1998" }));

        Llastname.setText("Lastname :");

        Snumero(TSSN);
        TSSN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TSSNActionPerformed(evt);
            }
        });

        Lbirthday.setText("Birthday :");

        CBmonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4","5", "6", "7", "8","9", "10", "11", "12" }));

        Lname.setText("Name :");

        LSSN.setText("SSN");

        Sletras(Tlastname);

        Lbirthplace.setText("Birthplace :");

        Bupdate.setText("Update");
        Bupdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BupdateActionPerformed(evt);
            }
        });

        Bdelete.setText("Delete");
        Bdelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BdeleteActionPerformed(evt);
            }
        });

        Lpetnames.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(LFocusofPractice, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(TFocusofPractice))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(LEspecialities)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(TEspecialities))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(LSalary)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(TSalary))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(LGraduatedAt)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(TGraduatedAt))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(Lbirthplace)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(Tbirthplace))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(Lbirthday)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(CBDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(CBmonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(CByear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(LSSN)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(TSSN))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(Llastname)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(Tlastname))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(Lname)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(Tname, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Bupdate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Bdelete))
                    .addComponent(Lpetnames))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lname)
                    .addComponent(Tname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Llastname)
                    .addComponent(Tlastname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LSSN)
                    .addComponent(TSSN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lbirthday)
                    .addComponent(CBDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CBmonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CByear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lbirthplace)
                    .addComponent(Tbirthplace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LFocusofPractice)
                    .addComponent(TFocusofPractice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LEspecialities)
                    .addComponent(TEspecialities, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LSalary)
                    .addComponent(TSalary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LGraduatedAt)
                    .addComponent(TGraduatedAt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Lpetnames)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Bupdate)
                    .addComponent(Bdelete))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void TnameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TnameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TnameActionPerformed

    private void TSSNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TSSNActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TSSNActionPerformed

    private void TFocusofPracticeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TFocusofPracticeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TFocusofPracticeActionPerformed

    private void BupdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BupdateActionPerformed
        // TODO add your handling code here:
        EntityManagerFactory emf;
        EntityManager em;
        Map<String, String> properties = new HashMap<>();
        emf = Persistence.createEntityManagerFactory("HPVAS");
        em = emf.createEntityManager();if (!this.Tlastname.getText().isEmpty())
            if (!this.Tbirthplace.getText().isEmpty())
                if (!this.TSalary.getText().isEmpty())
                    if (!this.TSSN.getText().isEmpty())
                        if (!this.TGraduatedAt.getText().isEmpty())
                            if (!this.TEspecialities.getText().isEmpty())
                                if (!this.TFocusofPractice.getText().isEmpty())
                                    if (!this.Tname.getText().isEmpty())
                                    { 
        
                                    Veterinarian Veterin=new Veterinarian(this.TFocusofPractice.getText(), Integer.parseInt(this.TSalary.getText()), this.TGraduatedAt.getText(),this.TEspecialities.getText(),this.Tname.getText(), this.Tlastname.getText(), this.TSSN.getText(), this.CByear.getSelectedItem().toString()+"-"+this.CBmonth.getSelectedItem().toString()+"-"+this.CBDay.getSelectedItem().toString()+" 00:00:00", this.Tbirthplace.getText());
                                        VeterinarianJpaController VeterinJpaC=new VeterinarianJpaController(emf);
                                        try {
                                            VeterinJpaC.edit(Veterin);
                                            JOptionPane.showMessageDialog(this, "Perfecto.");                                            
                                        } catch (PreexistingEntityException ex) {
                                            JOptionPane.showMessageDialog(this, "Esa Entidad no existia");
                                            Logger.getLogger(JPIVeterinarian.class.getName()).log(Level.SEVERE, null, ex);
                                        } catch (Exception ex) {
                                            JOptionPane.showMessageDialog(this, "Error durante la ejecucion");
                                            Logger.getLogger(JPIVeterinarian.class.getName()).log(Level.SEVERE, null, ex);
                                        }
                                    }
                                    else
                                        JOptionPane.showMessageDialog (this, "Name is empty");
                                else
                                    JOptionPane.showMessageDialog (this, "TFocusofPractice is empty");
                            else
                                JOptionPane.showMessageDialog (this, "TEspecialities is empty");
                        else
                            JOptionPane.showMessageDialog (this, "TGraduatedAt is empty");
                    else
                        JOptionPane.showMessageDialog (this, "TSSN is empty");
                else
                    JOptionPane.showMessageDialog (this, "TSalary is empty");
            else
                JOptionPane.showMessageDialog (this, "Tbirthplace is empty");
        else
                JOptionPane.showMessageDialog (this, "Lastname is empty");
    }//GEN-LAST:event_BupdateActionPerformed

    private void BdeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BdeleteActionPerformed
        EntityManagerFactory emf;
        EntityManager em;
        Map<String, String> properties = new HashMap<>();
        emf = Persistence.createEntityManagerFactory("HPVAS");
        em = emf.createEntityManager();         
        VeterinarianJpaController OwnJpaC=new VeterinarianJpaController(emf);
        try {
            OwnJpaC.destroy(Vet.getName());
            JOptionPane.showMessageDialog(this, "Perfecto.");
        } catch (NonexistentEntityException ex) {
            JOptionPane.showMessageDialog(this, "Esa Entidad no existia");
            Logger.getLogger(JPUOwner.class.getName()).log(Level.SEVERE, null, ex);
        }catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error durante la ejecucion");
            Logger.getLogger(JPIOwner.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_BdeleteActionPerformed
    private void TIdFocusGained(FocusEvent evt) {
        this.Tlastname.setEnabled(false);
        this.Tbirthplace.setEnabled(false);
        this.TSSN.setEnabled(false);
        this.CBDay.setEnabled(false);
        this.CBmonth.setEnabled(false);
        this.CByear.setEnabled(false);
        this.Bupdate.setEnabled(false);
        this.TSalary.setEnabled(false);
        this.TGraduatedAt.setEnabled(false);
        this.TEspecialities.setEnabled(false);
        this.TFocusofPractice.setEnabled(false);
    }    public void Snumero(JTextField a){
    a.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e){
        char c=e.getKeyChar();
        if (Character.isLetter(c)) {
            e.consume();
        }
    }
    });
    }
    public void Sletras(JTextField a){
    a.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e){
        char c=e.getKeyChar();
        if (Character.isDigit(c)) {
            e.consume();
        }
    }
    });
    } 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Bdelete;
    private javax.swing.JButton Bupdate;
    private javax.swing.JComboBox CBDay;
    private javax.swing.JComboBox CBmonth;
    private javax.swing.JComboBox CByear;
    private javax.swing.JLabel LEspecialities;
    private javax.swing.JLabel LFocusofPractice;
    private javax.swing.JLabel LGraduatedAt;
    private javax.swing.JLabel LSSN;
    private javax.swing.JLabel LSalary;
    private javax.swing.JLabel Lbirthday;
    private javax.swing.JLabel Lbirthplace;
    private javax.swing.JLabel Llastname;
    private javax.swing.JLabel Lname;
    private javax.swing.JLabel Lpetnames;
    private javax.swing.JTextField TEspecialities;
    private javax.swing.JTextField TFocusofPractice;
    private javax.swing.JTextField TGraduatedAt;
    private javax.swing.JTextField TSSN;
    private javax.swing.JTextField TSalary;
    private javax.swing.JTextField Tbirthplace;
    private javax.swing.JTextField Tlastname;
    private javax.swing.JTextField Tname;
    // End of variables declaration//GEN-END:variables
}
